/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package mnstudent.businessLayer;

import mnstudent.dataacesslayer.MajorDAL;
import mnstudent.datatranferobject.MajorDTO;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.jdesktop.swingx.JXTable;

/**
 *
 * @author 11520_000
 */
public class MajorBLL {
    
    MajorDAL majorDAL;
    
    public MajorBLL()
    {
        majorDAL = new MajorDAL();
    }
    
    /**
     * Ham lay tat ca cac nganh
     *
     * @return boolean
     */
    public ResultSet getAllMajor() {
      
        return majorDAL.getAllMajor();
    }
    
    public ResultSet getAllMajorWithNameOfFaculty()
    {
      
        return majorDAL.getAllMajorWithNameOfFaculty();
    }
    
    public ArrayList<MajorDTO> getAllMajorToArray()
    {
        ArrayList<MajorDTO> temp = new ArrayList<>();
        ResultSet rs = getAllMajor();
        try {
            while(rs.next())
            {
                MajorDTO major = new MajorDTO(rs.getNString(1), rs.getNString(2), rs.getNString(3));
                temp.add(major);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;   
    }
    /**
     * Ham lay khoa theo ma khoa
     * @param _id
     * @return tra ve table chon
     */
    public ResultSet getMajorById(String _id)
    {
      
        return majorDAL.getMajorById(_id);
    }
    
    public ArrayList<MajorDTO> getAllMajorByFacultyIDToArray(String _facultyID){
        
        ArrayList<MajorDTO> temp = new ArrayList<>();
        ResultSet rs = majorDAL.getAllMajorByFacultyID(_facultyID);
        try {
            while(rs.next())
            {
                MajorDTO major = new MajorDTO(rs.getNString(1), rs.getNString(2), rs.getNString(3));
                temp.add(major);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return temp;  
    }
    
//    public ResultSet getAllMajorByFacultyID(String _facultyID)
//    {
//        return majorDAL.getNameOfFacultyByMajorIdFromDatabase(_facultyID);
//    }
    
    public ResultSet getNameOfFacultyByMajorIdFromDatabase(String _idMajor)
    {
        return majorDAL.getNameOfFacultyByMajorIdFromDatabase(_idMajor);
    }
     /**
     * ham them moi khoa xuong database
     * @param _major
     * @return true: success, false: fail
     */
    public boolean insertMajorToDatabase(MajorDTO _major)
    {
       return majorDAL.insertMajorToDatabase(_major);
    }
    
    /**
     * Ham cap nhat sua doi thong tin khoa
     * @param _major
     * @return tue: thanh cong, false: that bai
     */
    public boolean updateMajorToDatabase(MajorDTO _major)
    {
        return majorDAL.updateMajorToDatabase(_major);
    }
    
    /**
     * Ham xoa mot khoa ra khoi database
     * @param _major
     * @return 
     */
     public boolean deleteMajorFromDatabase(MajorDTO _major)
    {
            
        return majorDAL.deleteMajorFromDatabase(_major);
    }
     
    public boolean checkInputIsInvalid(JXTable _jtb, String _input)
     {
        for(int i = 0; i<_jtb.getRowCount(); i++)
        {
            if(_jtb.getSelectedRow() != i){

                if(_input.replaceAll("\\s+", "").toLowerCase().equals(_jtb.getValueAt(i, 1).toString().replaceAll("\\s+", "").toLowerCase())){
                    return false;
                }
            }
        }
          return true;
     }
}
